package com.wangdao.mall.dao;

import com.wangdao.mall.model.Admin;
import com.wangdao.mall.model.Password;
import com.wangdao.mall.model.User;
import com.wangdao.mall.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:HaiyangTian
 * @Description:
 * @Date: Created in 16:44 2020/4/30
 * @Modified By:
 */
public class UserDaoImp implements UserDao{

    /**
     * 查询所有用户信息
     * @return
     */
    @Override
    public List<User> allUser() {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        List<User> users = null;
        try {
            users = runner.query("select * from user",
                    new BeanListHandler<User>(User.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return users;
    }

    /**
     * 从数据库中删除数据
     * @param id
     * @return
     */
    @Override
    public int deleteUser(String id) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        int row = 0;
        try {
            row = runner.update("delete from user where id = ?", id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return row;
    }

    /**
     * 从数据库搜索指定用户
     * @param nickname
     * @return
     */
    @Override
    public List<User> searchUser(String nickname) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        List<User> users = null;
        try {
            users = runner.query("select * from user where nickname like ?",
                    new BeanListHandler<User>(User.class), "%" + nickname + "%");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return users;
    }

    /**
     * 查询user表数据
     * @param user
     * @return
     */
    @Override
    public int login(User user) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        try {
            User query = runner.query("select * from user where email = ? and pwd = ?",
                    new BeanHandler<>(User.class),
                    user.getEmail(), user.getPwd());
            user.setNickname(query.getNickname());
            if (query != null){
                return  200;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 500;
    }

    /**
     * 向用户表中插入信息
     * @param user
     * @return
     */
    @Override
    public int signup(User user) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        int row = 0;
        try {
            row = runner.update("insert into user values(null,?,?,?,?,?,?)",
                    user.getEmail(),
                    user.getNickname(),
                    user.getPwd(),
                    user.getRecipient(),
                    user.getAddress(),
                    user.getPhone());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return row;
    }

    /**
     * 获取用户信息
     * @param token
     * @return
     */
    @Override
    public User getUserData(String token) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        User user = null;
        try {
            user = runner.query("select * from user where nickname = ?",
                    new BeanHandler<>(User.class), token);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }

    /**
     * 更新用户信息
     * @param user
     * @return
     */
    @Override
    public void updateUserData(User user) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        Map<String,Object> map = getInsertSql(user);
        String sql = (String) map.get("sql");
        List<Object> params = (List<Object>) map.get("params");
        try {
            runner.update(sql, params.toArray());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int updatePwd(Password password) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        int row = 0;
        try {
            String oldPwd = runner.query("select pwd from user where id = ?",
                    new ScalarHandler<>(), password.getId());
            if (!oldPwd.equals(password.getOldPwd())) {
                return row;
            }
            row = runner.update("update user set pwd = ? where id = ?",
                    password.getNewPwd(),password.getId());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return row;
    }

    /**
     * 获取更新的语句
     * @param user
     * @return
     */
    private Map<String, Object> getInsertSql(User user) {
        Map<String, Object> map = new HashMap<>();
        List<Object> params = new ArrayList<>();
        String baseSql = "update user set ";
        baseSql = baseSql + "nickname = ? ,";
        params.add(user.getNickname());
        baseSql = baseSql + "recipient = ?,";
        params.add(user.getRecipient());
        baseSql = baseSql + "address = ?,";
        params.add(user.getAddress());
        baseSql = baseSql + "phone = ? ";
        params.add(user.getPhone());
        baseSql = baseSql + "where id = ?";
        params.add(user.getId());
        map.put("sql",baseSql);
        map.put("params",params);
        return map;
    }
}
